package com.masterclient.app.modules.purchase.widget;

import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.widget.EditText;

/**
 * Created by yongqianggeng on 2018/4/16.
 */

public class LiveInputLengthWatcher implements TextWatcher {

    private int maxLen = 0;
    private EditText editText = null;


    public LiveInputLengthWatcher(int maxLen, EditText editText) {
        this.maxLen = maxLen;
        this.editText = editText;
    }

    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
        // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        Editable editable = editText.getText();
        int len = editable.length();

        int max = maxLen;
        if(len > max)
        {
            //TODO:emoji特殊处理
            int selEndIndex = Selection.getSelectionEnd(editable);
            String str = editable.toString();
            boolean isHavelastEmoji = false;
            char codePoint = str.charAt(str.length()-1);
            if (!isEmojiCharacter(codePoint)) {
                isHavelastEmoji = true;
            }
            //截取新字符串
            String newStr = str.substring(0,max);
            if (isHavelastEmoji) {
                if (arg3 == len) {
                    try {
//                        ToastUtil.showToast("最多发送"+maxLen+"个字符");
                    } catch (Exception e) {

                    }
                    new android.os.Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            editText.dispatchKeyEvent(new KeyEvent(0, 67));
                        }
                    },1);

                }

                return;
            }
            editText.setText(newStr);
            editable = editText.getText();

            //新字符串的长度
            int newLen = editable.length();
            //旧光标位置超过字符串长度
            if(selEndIndex > newLen)
            {
                selEndIndex = editable.length();
            }
            //设置新光标所在的位置
            Selection.setSelection(editable, selEndIndex);
            try {
//                ToastUtil.showToast("最多发送"+maxLen+"个字符");
            } catch (Exception e) {

            }

        }
    }

    /**
     * 判断是否是Emoji
     *
     * @param codePoint 比较的单个字符
     * @return
     */

    private static boolean isEmojiCharacter(char codePoint) {

        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
                (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
                && (codePoint <= 0x10FFFF));
    }

}